package com.chezhujie.www.controller;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjorderdistribution;
import com.chezhujie.www.service.CzjorderdistributionService;
import com.chezhujie.www.utils.ConvertObj;
import com.chezhujie.www.utils.Fileupload;
import com.chezhujie.www.utils.UUIDUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 *   订单配送表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjorderdistribution")
public class CzjorderdistributionController {
    @Autowired
    private CzjorderdistributionService czjorderdistributionService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjorderdistribution czjorderdistribution, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorderdistribution,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjorderdistribution);
            Pagination pagination = czjorderdistributionService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取订单配送表列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjorderdistribution);
        return "czjorderdistribution/list";
    }
    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjorderdistribution czjorderdistribution,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjorderdistribution.getId())){
                czjorderdistributionService.update(czjorderdistribution);


            }else{
                czjorderdistribution.setId(UUIDUtils.getUUID());
                czjorderdistributionService.insert(czjorderdistribution);
            }
            model.addAttribute("success", "保存订单配送表成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存订单配送表失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String id,Czjorderdistribution czjorderdistribution, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorderdistribution,req,model);
        try {
            Czjorderdistribution entity = czjorderdistributionService.queryById(czjorderdistribution.getId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询订单配送表出错!详情："+e.getMessage());
        }
        return "czjorderdistribution/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjorderdistribution czjorderdistribution, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjorderdistribution,req,model);
        if(StringUtils.isNotBlank(czjorderdistribution.getId())){
            czjorderdistribution = czjorderdistributionService.queryById(czjorderdistribution.getId());
            model.addAttribute("entity", czjorderdistribution);
        }
        return "czjorderdistribution/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String id,Czjorderdistribution czjorderdistribution, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjorderdistributionService.deleteById(czjorderdistribution.getId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(Czjorderdistribution czjorderdistribution,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjorderdistribution);
    }
}
